﻿<?php
	set_time_limit(0);
	require_once("config.inc.php");
	require_once("charset.func.php");
	require_once("db.class.php");
	$db = new db($site_Db);
	
	$startyear = 1600;
	$endyear = 2099;
	$fields = array(
		'year'=>urlencode('2010'),
		'mm'=>urlencode('1')
	);
	$url = 'http://www.nongli.com/item3/index.asp';
	echo "*** get cal start.***<br/>\n";
	for($y = 2000;$y<2100;$y++) {
		for($m = 1;$m<13;$m++) {
			$fields['year'] = urlencode(strval($y));
			$fields['mm'] = urlencode(strval($m));
			$fields_string = '';
			//url-ify the data for the POST
			foreach($fields as $key=>$value) {
				$fields_string .= $key.'='.$value.'&';
			}
			rtrim($fields_string ,'&');
			//echo $fields_string;
			//ob_start
			ob_start();
			//open connection
			$ch = curl_init();
			//set the url, number of POST vars, POST data 
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_POST,count($fields));
			curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
			//execute post
			curl_exec($ch);
			
			$result=ob_get_contents();
			ob_end_clean();
			//提取万年历信息
			//$result = read_File('./r.txt');
			//write_File('./r.txt',$result);
			$result = gb2utf8($result);
			preg_match_all("/<A\sHREF=\"#\"\sonclick=return\(false\)\stitle=\".+?公历:(.+?)\s(.+?)\">.+?<\/A>/s",$result,$list,PREG_SET_ORDER);
			//print_r($list);
			
			foreach($list as $i) {
				if(!empty($i[1]) && !empty($i[2])) {
					$info=preg_replace("/(&nbsp;)+/", " ", $i[2]);
					if($db->query("REPLACE INTO cal20 VALUES('".trim($i[1])."', '".trim($info)."')"))
						echo "********* Add a new date {$i[1]}.<br/>\n";
				}
			}
			echo "****** Add a new month {$y}-{$m}.<br/>\n";
		}
		echo "*** Add a new year {$y}.<br/>\n";
		sleep(1);
	}
	
?>